cmake_minimum_required(VERSION 3.1)
project(slam_karto)

if (NOT CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 11)
endif ()

find_package(catkin REQUIRED
        COMPONENTS
        message_filters
        nav_msgs
        rosconsole
        roscpp
        sensor_msgs
        tf
        visualization_msgs
        )
find_package(Eigen3 REQUIRED)
find_package(suitesparse QUIET)
find_package(Boost REQUIRED COMPONENTS thread)

if (BUILD_SHARED_LIBS)
    add_definitions(-DKARTO_DYNAMIC)
endif ()
if (NOT SuiteSparse_FOUND)
    set(SuiteSparse_LIBRARIES blas lapack cholmod cxsparse)
endif ()

include_directories(
        include
        "/usr/include/eigen3"
        ${catkin_INCLUDE_DIRS}
        ${Boost_INCLUDE_DIRS}
        ${CSPARSE_DIR}/CSparse/Include
        ${SBA_DIR}/sba/include
        ${SuiteSparse_INCLUDE_DIRS}
)

catkin_package(
        DEPENDS Boost
        INCLUDE_DIRS include
        include
        LIBRARIES sba
        karto
)

add_definitions(
        -DSBA_CHOLMOD
)

set(SPARSE_BUNDLE_ADJUSTMENT
        src/sparse_bundle_adjustment/sba.cpp
        src/sparse_bundle_adjustment/spa.cpp
        src/sparse_bundle_adjustment/spa2d.cpp
        src/sparse_bundle_adjustment/csparse.cpp
        src/sparse_bundle_adjustment/proj.cpp
        src/sparse_bundle_adjustment/sba_file_io.cpp
        src/sparse_bundle_adjustment/node.cpp)

# SBA library
add_library(sba ${SPARSE_BUNDLE_ADJUSTMENT})
#rosbuild_add_compile_flags(sba ${SSE_FLAGS})
target_link_libraries(sba ${SuiteSparse_LIBRARIES})
# karto library
add_library(karto SHARED src/open_karto/Karto.cpp src/open_karto/Mapper.cpp)
target_link_libraries(karto ${Boost_LIBRARIES})
# slam_karto
add_executable(slam_karto src/slam_karto.cpp src/spa_solver.cpp)
target_link_libraries(slam_karto ${catkin_LIBRARIES} sba karto)
# slam_karto
#add_executable(sba_test_node src/sba_test_node.cpp)
#add_dependencies(sba_test_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
#target_link_libraries(sba_test_node ${catkin_LIBRARIES} sba karto)

# test
ADD_DEFINITIONS(-DUSE_SPA)
if (WIN32)
    ADD_DEFINITIONS(-DEIGEN_DONT_ALIGN)
endif ()
if (WIN32)
    # turn off various warnings
    foreach (warning 4251)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${warning}")
    endforeach (warning)
endif ()
############################################################################
# tutorial 1
add_executable(Karto_Tutorial1 samples/tutorial1.cpp)
target_link_libraries(Karto_Tutorial1 karto)
if (UNIX)
    target_link_libraries(Karto_Tutorial1 "pthread")
endif ()
############################################################################
# tutorial 2
add_executable(Karto_Tutorial2 samples/tutorial2.cpp samples/SpaSolver.h samples/SpaSolver.cpp)
target_link_libraries(Karto_Tutorial2 karto sba)
if (PNG_FOUND)
    target_link_libraries(Karto_Tutorial2 ${ZLIB_LIBRARIES})
    target_link_libraries(Karto_Tutorial2 ${PNG_LIBRARIES})
endif ()
if (UNIX)
    target_link_libraries(Karto_Tutorial2 "pthread")
endif ()

install(TARGETS slam_karto sba karto
        LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
        ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
        RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
        )

install(DIRECTORY include/sparse_bundle_adjustment/ include/  DESTINATION include
        DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION})

